package ps.client.gui.util;

import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.prefs.Preferences;

import javax.swing.JDialog;
import javax.swing.JOptionPane;


@SuppressWarnings("serial")
public class DialogEx extends JDialog {
	
	public static final String WINDOW_LOCATION_X = "WindowLocationX";
	public static final String WINDOW_LOCATION_Y = "WindowLocationY";
	public static final String WINDOW_WIDTH = "WindowWidth";
	public static final String WINDOW_HEIGHT = "WindowHeight";
	public static final String WINDOW_IS_VISIBLE = "WindowIsVisible";
	
	Preferences prefNode;
	
	Window superOwner;
	
	WindowAdapter windowAdapter = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            saveSettings();
        }
    };

	public DialogEx(Dialog owner, String prefNodeName) {
		super(owner);
		init(prefNodeName);
	}
	
	public DialogEx(Frame owner, String prefNodeName) {
		super(owner);
		init(prefNodeName);
	}
	
	protected void init(String prefNodeName) {
		this.prefNode = Preferences.userRoot().node(prefNodeName);;
		
		superOwner = getOwner();
		if (superOwner != null) {
    		while (superOwner.getOwner() != null) {
    			superOwner = superOwner.getOwner();
    		}
    		superOwner.addWindowListener(windowAdapter);
		}
		loadSettings();
	}
	
	public Preferences getPrefNode() {
		return prefNode;
	}
	
	public void loadSettings() {
	    Window owner = getOwner();
	    if (owner != null) {
    		setLocation(
    				prefNode.getInt(WINDOW_LOCATION_X, owner.getLocation().x + 50),
    				prefNode.getInt(WINDOW_LOCATION_Y, owner.getLocation().y + 50));
	    } else {
	        setLocation(
                    prefNode.getInt(WINDOW_LOCATION_X, 50),
                    prefNode.getInt(WINDOW_LOCATION_Y, 50));
	    }

	    setSize(prefNode.getInt(WINDOW_WIDTH, 650), prefNode.getInt(WINDOW_HEIGHT, 400));
	    
//	    int posX = prefNode.getInt(WINDOW_LOCATION_X, -1);
//        int posY = prefNode.getInt(WINDOW_LOCATION_Y, -1);
//        if (posX > -1 && posY > -1) {
//            setLocation(posX, posY);
//        } else {
//            centerDialogOnScreen();
//        }
	}
	
	public void saveSettings() {
		prefNode.putInt(WINDOW_LOCATION_X, getLocation().x);
		prefNode.putInt(WINDOW_LOCATION_Y, getLocation().y);
		prefNode.putInt(WINDOW_WIDTH, getSize().width);
		prefNode.putInt(WINDOW_HEIGHT, getSize().height);
		prefNode.putBoolean(WINDOW_IS_VISIBLE, isVisible());
	}
	
	public void setVisibility() {
		setVisible(prefNode.getBoolean(WINDOW_IS_VISIBLE, false));
	}
	
	protected void clean() {
	    if (superOwner != null) {
	        superOwner.removeWindowListener(windowAdapter);
	    }
	}
	
	public void showErrorMsg(String msg) {
	    JOptionPane.showMessageDialog(this, msg, getTitle(), JOptionPane.ERROR_MESSAGE);
	}
	
	protected void centerDialogOnScreen() {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension dialogSize = getSize();
        if (dialogSize.height > screenSize.height) {
            dialogSize.height = screenSize.height - 100;
        }
        if (dialogSize.width > screenSize.width) {
            dialogSize.width = screenSize.width - 100;
        }
        setLocation((screenSize.width - dialogSize.width) / 2,
                (screenSize.height - dialogSize.height) / 2);
    }
}
